<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mockiframeio.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_mockiframeio.js.html">mockiframeio.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Mock of IframeIo for unit testing.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.net.MockIFrameIo&#39;);
<a name="line20"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line21"></a>goog.require(&#39;goog.net.ErrorCode&#39;);
<a name="line22"></a>goog.require(&#39;goog.net.IframeIo&#39;);
<a name="line23"></a>goog.require(&#39;goog.net.IframeIo.IncrementalDataEvent&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Mock implenetation of goog.net.IframeIo. This doesn&#39;t provide a mock
<a name="line28"></a> * implementation for all cases, but it&#39;s not too hard to add them as needed.
<a name="line29"></a> * @param {goog.testing.TestQueue} testQueue Test queue for inserting test
<a name="line30"></a> *     events.
<a name="line31"></a> * @constructor
<a name="line32"></a> * @extends {goog.events.EventTarget}
<a name="line33"></a> */
<a name="line34"></a>goog.net.MockIFrameIo = function(testQueue) {
<a name="line35"></a>  goog.events.EventTarget.call(this);
<a name="line36"></a>
<a name="line37"></a>  /**
<a name="line38"></a>   * Queue of events write to
<a name="line39"></a>   * @type {goog.testing.TestQueue}
<a name="line40"></a>   * @private
<a name="line41"></a>   */
<a name="line42"></a>  this.testQueue_ = testQueue;
<a name="line43"></a>
<a name="line44"></a>};
<a name="line45"></a>goog.inherits(goog.net.MockIFrameIo, goog.events.EventTarget);
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Whether MockIFrameIo is active.
<a name="line50"></a> * @type {boolean}
<a name="line51"></a> * @private
<a name="line52"></a> */
<a name="line53"></a>goog.net.MockIFrameIo.prototype.active_ = false;
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Last error code.
<a name="line58"></a> * @type {goog.net.ErrorCode}
<a name="line59"></a> * @private
<a name="line60"></a> */
<a name="line61"></a>goog.net.MockIFrameIo.prototype.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Last error message.
<a name="line66"></a> * @type {string}
<a name="line67"></a> * @private
<a name="line68"></a> */
<a name="line69"></a>goog.net.MockIFrameIo.prototype.lastError_ = &#39;&#39;;
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Simulates the iframe send.
<a name="line73"></a> *
<a name="line74"></a> * @param {goog.Uri|string} uri Uri of the request.
<a name="line75"></a> * @param {string} opt_method Default is GET, POST uses a form to submit the
<a name="line76"></a> *     request.
<a name="line77"></a> * @param {boolean} opt_noCache Append a timestamp to the request to avoid
<a name="line78"></a> *     caching.
<a name="line79"></a> * @param {Object|goog.structs.Map} opt_data Map of key-value pairs.
<a name="line80"></a> */
<a name="line81"></a>goog.net.MockIFrameIo.prototype.send = function(uri, opt_method, opt_noCache,
<a name="line82"></a>                                                opt_data) {
<a name="line83"></a>  if (this.active_) {
<a name="line84"></a>    throw Error(&#39;[goog.net.IframeIo] Unable to send, already active.&#39;);
<a name="line85"></a>  }
<a name="line86"></a>
<a name="line87"></a>  this.testQueue_.enqueue([&#39;s&#39;, uri, opt_method, opt_noCache, opt_data]);
<a name="line88"></a>  this.complete_ = false;
<a name="line89"></a>  this.active_ = true;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Simulates the iframe send from a form.
<a name="line95"></a> * @param {Element} form Form element used to send the request to the server.
<a name="line96"></a> * @param {string} opt_uri Uri to set for the destination of the request, by
<a name="line97"></a> *     default the uri will come from the form.
<a name="line98"></a> * @param {boolean} opt_noCache Append a timestamp to the request to avoid
<a name="line99"></a> *     caching.
<a name="line100"></a> */
<a name="line101"></a>goog.net.MockIFrameIo.prototype.sendFromForm = function(form, opt_uri,
<a name="line102"></a>     opt_noCache) {
<a name="line103"></a>  if (this.active_) {
<a name="line104"></a>    throw Error(&#39;[goog.net.IframeIo] Unable to send, already active.&#39;);
<a name="line105"></a>  }
<a name="line106"></a>
<a name="line107"></a>  this.testQueue_.enqueue([&#39;s&#39;, form, opt_uri, opt_noCache]);
<a name="line108"></a>  this.complete_ = false;
<a name="line109"></a>  this.active_ = true;
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Simulates aborting the current Iframe request.
<a name="line114"></a> * @param {goog.net.ErrorCode} opt_failureCode Optional error code to use -
<a name="line115"></a> *     defaults to ABORT.
<a name="line116"></a> */
<a name="line117"></a>goog.net.MockIFrameIo.prototype.abort = function(opt_failureCode) {
<a name="line118"></a>  if (this.active_) {
<a name="line119"></a>    this.testQueue_.enqueue([&#39;a&#39;, opt_failureCode]);
<a name="line120"></a>    this.complete_ = false;
<a name="line121"></a>    this.active_ = false;
<a name="line122"></a>    this.success_ = false;
<a name="line123"></a>    this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT;
<a name="line124"></a>    this.dispatchEvent(goog.net.EventType.ABORT);
<a name="line125"></a>    this.simulateReady();
<a name="line126"></a>  }
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Simulates receive of incremental data.
<a name="line131"></a> * @param {Object} data Data.
<a name="line132"></a> */
<a name="line133"></a>goog.net.MockIFrameIo.prototype.simulateIncrementalData = function(data) {
<a name="line134"></a>  this.dispatchEvent(new goog.net.IframeIo.IncrementalDataEvent(data));
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Simulates the iframe is done.
<a name="line140"></a> * @param {goog.net.ErrorCode} errorCode The error code for any error that
<a name="line141"></a> *     should be simulated.
<a name="line142"></a> */
<a name="line143"></a>goog.net.MockIFrameIo.prototype.simulateDone = function(errorCode) {
<a name="line144"></a>  if (errorCode) {
<a name="line145"></a>    this.success_ = false;
<a name="line146"></a>    this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR;
<a name="line147"></a>    this.lastError_ = this.getLastError();
<a name="line148"></a>    this.dispatchEvent(goog.net.EventType.ERROR);
<a name="line149"></a>  } else {
<a name="line150"></a>    this.success_ = true;
<a name="line151"></a>    this.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
<a name="line152"></a>    this.dispatchEvent(goog.net.EventType.SUCCESS);
<a name="line153"></a>  }
<a name="line154"></a>  this.complete_ = true;
<a name="line155"></a>  this.dispatchEvent(goog.net.EventType.COMPLETE);
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * Simulates the IFrame is ready for the next request.
<a name="line160"></a> */
<a name="line161"></a>goog.net.MockIFrameIo.prototype.simulateReady = function() {
<a name="line162"></a>  this.dispatchEvent(goog.net.EventType.READY);
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * @return {boolean} True if transfer is complete.
<a name="line168"></a> */
<a name="line169"></a>goog.net.MockIFrameIo.prototype.isComplete = function() {
<a name="line170"></a>  return this.complete_;
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * @return {boolean} True if transfer was successful.
<a name="line176"></a> */
<a name="line177"></a>goog.net.MockIFrameIo.prototype.isSuccess = function() {
<a name="line178"></a>  return this.success_;
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * @return {boolean} True if a transfer is in progress.
<a name="line184"></a> */
<a name="line185"></a>goog.net.MockIFrameIo.prototype.isActive = function() {
<a name="line186"></a>  return this.active_;
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Returns the last response text (i.e. the text content of the iframe).
<a name="line192"></a> * Assumes plain text!
<a name="line193"></a> * @return {string} Result from the server.
<a name="line194"></a> */
<a name="line195"></a>goog.net.MockIFrameIo.prototype.getResponseText = function() {
<a name="line196"></a>  return this.lastContent_;
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Parses the content as JSON. This is a safe parse and may throw an error
<a name="line202"></a> * if the response is malformed.
<a name="line203"></a> * @return {Object} The parsed content.
<a name="line204"></a> */
<a name="line205"></a>goog.net.MockIFrameIo.prototype.getResponseJson = function() {
<a name="line206"></a>  return goog.json.parse(this.lastContent_);
<a name="line207"></a>};
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Get the uri of the last request.
<a name="line212"></a> * @return {goog.Uri} Uri of last request.
<a name="line213"></a> */
<a name="line214"></a>goog.net.MockIFrameIo.prototype.getLastUri = function() {
<a name="line215"></a>  return this.lastUri_;
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/**
<a name="line220"></a> * Gets the last error code.
<a name="line221"></a> * @return {goog.net.ErrorCode} Last error code.
<a name="line222"></a> */
<a name="line223"></a>goog.net.MockIFrameIo.prototype.getLastErrorCode = function() {
<a name="line224"></a>  return this.lastErrorCode_;
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * Gets the last error message.
<a name="line230"></a> * @return {string} Last error message.
<a name="line231"></a> */
<a name="line232"></a>goog.net.MockIFrameIo.prototype.getLastError = function() {
<a name="line233"></a>  return goog.net.ErrorCode.getDebugMessage(this.lastErrorCode_);
<a name="line234"></a>};
<a name="line235"></a>
<a name="line236"></a>
<a name="line237"></a>/**
<a name="line238"></a> * Gets the last custom error.
<a name="line239"></a> * @return {Object} Last custom error.
<a name="line240"></a> */
<a name="line241"></a>goog.net.MockIFrameIo.prototype.getLastCustomError = function() {
<a name="line242"></a>  return this.lastCustomError_;
<a name="line243"></a>};
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * Sets the callback function used to check if a loaded IFrame is in an error
<a name="line248"></a> * state.
<a name="line249"></a> * @param {Function} fn Callback that expects a document object as it&#39;s single
<a name="line250"></a> *     argument.
<a name="line251"></a> */
<a name="line252"></a>goog.net.MockIFrameIo.prototype.setErrorChecker = function(fn) {
<a name="line253"></a>  this.errorChecker_ = fn;
<a name="line254"></a>};
<a name="line255"></a>
<a name="line256"></a>
<a name="line257"></a>/**
<a name="line258"></a> * Gets the callback function used to check if a loaded IFrame is in an error
<a name="line259"></a> * state.
<a name="line260"></a> * @return {Function} A callback that expects a document object as it&#39;s single
<a name="line261"></a> *     argument.
<a name="line262"></a> */
<a name="line263"></a>goog.net.MockIFrameIo.prototype.getErrorChecker = function() {
<a name="line264"></a>  return this.errorChecker_;
<a name="line265"></a>};
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>/**
<a name="line269"></a> * Returns the number of milliseconds after which an incomplete request will be
<a name="line270"></a> * aborted, or 0 if no timeout is set.
<a name="line271"></a> * @return {number} Timeout interval in milliseconds.
<a name="line272"></a> */
<a name="line273"></a>goog.net.MockIFrameIo.prototype.getTimeoutInterval = function() {
<a name="line274"></a>  return this.timeoutInterval_;
<a name="line275"></a>};
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * Sets the number of milliseconds after which an incomplete request will be
<a name="line280"></a> * aborted and a {@link goog.net.EventType.TIMEOUT} event raised; 0 means no
<a name="line281"></a> * timeout is set.
<a name="line282"></a> * @param {number} ms Timeout interval in milliseconds; 0 means none.
<a name="line283"></a> */
<a name="line284"></a>goog.net.MockIFrameIo.prototype.setTimeoutInterval = function(ms) {
<a name="line285"></a>  // TODO (pupius) - never used - doesn&#39;t look like timeouts were implemented
<a name="line286"></a>  this.timeoutInterval_ = Math.max(0, ms);
<a name="line287"></a>};
</pre>


</body>
</html>
